package com.tom.architect.day20.simple1;

import java.util.HashMap;
import java.util.Map;

/**
 * Added by Tom on 2023/12/26.
 * 火车票的查询工厂
 */
public class TicketFactory {
    // 缓存对象。
    static Map<String, Ticket> sTicketMap = new HashMap<>();

    public static Ticket getTicket(String from, String to) {
        String key = from + "-" + to;
        Ticket ticket = sTicketMap.get(key);
        if (ticket != null) {
            // 非空，直接返回。
            return ticket;
        }
        ticket = new Ticket(from, to);
        sTicketMap.put(key, ticket); // 添加到集合中。
        return ticket;
    }

}
